package com.cdn.plugin.depress;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 蔡定努
 * 2024/08/25 22:15
 */
public class ClassPathDataCopier {
    public  void copyData(String source, String destination) {
        URL resource = getClass().getResource(source);
        if (resource == null) {
            return;
        }
        Path path = Paths.get(destination);
        if (!path.getParent().toFile().exists()) path.getParent().toFile().mkdirs();

        try (InputStream inputStream = resource.openStream()) {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            int nRead;
            byte[] data = new byte[8092];
            while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, nRead);
            }
            Files.write(path, buffer.toByteArray());
        } catch (IOException ignored) {
        }
    }
}
